package com.liyao.storehouse.service.impl;

import com.liyao.storehouse.dao.storageDao;
import com.liyao.storehouse.pojo.storage.*;
import com.liyao.storehouse.service.storageService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class storageServiceImpl implements storageService {
    @Resource
    storageDao dao;
    @Override
    public JSONObject getCheckList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<checkList> list=dao.getCheckList((page-1)*limit,limit);
        for(checkList c:list){
            js.put("id",c.getId());
            js.put("checkId",c.getCheckId());
            js.put("device",c.getDevice());
            js.put("dept",c.getDept());
            js.put("workbenches",c.getWorkbenches());
            js.put("status",c.getStatus());
            js.put("username",c.getUsername());
            js.put("updateAtStr",c.getUpdateAtStr());
            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getCheckListCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getGoodsList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<supplies> list=dao.getGoodsList((page-1)*limit,limit);
        for(supplies c:list){
            js.put("id",c.getId());
            js.put("productCode",c.getProductCode());
            js.put("productName",c.getProductName());
            js.put("expectNum",c.getExpectNum());
            js.put("overfillNum",c.getOverfillNum());
            js.put("inventoryNum",c.getInventoryNum());
            js.put("containerNum",c.getNum());
            arr.add(js);
        }
        int x= dao.getGoodsListCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getGoodsList1(int page, int limit, String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<supplies> list=dao.getGoodsList1((page-1)*limit,limit,id.split(","));
        for(supplies c:list){
            js.put("id",c.getId());
            js.put("productCode",c.getProductCode());
            js.put("productName",c.getProductName());
            js.put("expectNum",c.getExpectNum());
            js.put("overfillNum",c.getOverfillNum());
            arr.add(js);
        }
        int x= dao.getGoodsListCount1(id.split(","));
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getStorageList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<store> list=dao.getStorageList((page-1)*limit,limit);
        for(store c:list){
            js.put("id",c.getId());
            js.put("storeId",c.getStoreId());
            js.put("device",c.getDevice());
            js.put("dept",c.getDept());
            js.put("workbenches",c.getWorkbenches());
            js.put("status",c.getStatus());
            js.put("username",c.getUsername());
            js.put("updateAtStr",c.getUpdateAtStr());
            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getStorageListCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getContainerList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<container> list=dao.getContainerList((page-1)*limit,limit);
        for(container c:list){
            js.put("id",c.getId());
            js.put("ContainerCode",c.getContainerCode());
            js.put("device",c.getDevice());
            js.put("arrange",c.getArrange());
            js.put("row",c.getRow());
            js.put("status",c.getStatus());
            js.put("usageRate",c.getUsageRate());
            js.put("updateAtStr",c.getUpdateAtStr());
            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getContainerListCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getContainerList1(int page, int limit, String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<container> list=dao.getContainerList1((page-1)*limit,limit,id);
        for(container c:list){
            js.put("id",c.getId());
            js.put("ContainerCode",c.getContainerCode());
            js.put("device",c.getDevice());
            js.put("actualCount",c.getActualCount());
//            js.put("arrange",c.getArrange());
//            js.put("row",c.getRow());
//            js.put("status",c.getStatus());
            js.put("usageRate",c.getUsageRate());
//            js.put("updateAtStr",c.getUpdateAtStr());
//            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getContainerListCount1(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getContainerAndGoodsList(int page, int limit, String goodsId) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<container> list=dao.getContainerAndGoodsList((page-1)*limit,limit,goodsId);
        for(container c:list){
            js.put("id",c.getId());
            js.put("ContainerCode",c.getContainerCode());
            js.put("device",c.getDevice());
            js.put("arrange",c.getArrange());
            js.put("inventoryNum",c.getInventoryNum());
            js.put("row",c.getRow());
            js.put("status",c.getStatus());
            js.put("usageRate",c.getUsageRate());
            js.put("updateAtStr",c.getUpdateAtStr());
            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getContainerAndGoodsListCount(goodsId);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject addSupplies(String productCode, String productName, String expectNum, String overfillNum) {
        JSONObject js = new JSONObject();
        int x=dao.getProductCode(productCode);
        if (x>0){
            js.put("code","1060");
        }else {
            int y=dao.insertSupplies(productCode,productName,expectNum,overfillNum);
            js.put("code","suss");
        }
        return js;
    }

    @Override
    public JSONObject DelSupplies(String id) {
        JSONObject js=new JSONObject();
        int x=dao.DelSupplies(id);
            js.put("code","suss");
        return js;
    }

    @Override
    public JSONObject editSupplies(String id, String productName, String expectNum, String overfillNum) {
        JSONObject js = new JSONObject();
        int x=dao.updateSupplies(id,productName,expectNum,overfillNum);
        js.put("code","suss");
        return js;
    }

    @Override
    public JSONObject addStorage(String id, String overfill) {
        JSONObject js=new JSONObject();
        store store = new store();
        store.setDept("BH");
        int x=dao.insertStorage(store);
        System.out.println("===="+store.getId());
        String[] ids=id.split(",");
        String[] overfill1 = overfill.split(",");
        for (int i = 1; i < ids.length; i++) {
            int y=dao.insertStoreDetails(ids[i],overfill1[i],store.getId());

        }
        return null;
    }

    @Override
    public JSONObject addStorage1(String id, String overfill, String containerId) {
        JSONObject js=new JSONObject();
        store store = new store();
        store.setDept("BH");
        int x=dao.insertStorage(store);
        System.out.println("===="+store.getId());
        String[] ids=id.split(",");
        String[] overfill1 = overfill.split(",");
        String[] container = containerId.split("-");
        for (int i = 1; i < ids.length; i++) {
            System.out.println(ids[i]+"==");
            System.out.println(overfill1[i]+"--");

//            Map<String, Object> outerMap = new HashMap<>();
//            outerMap.put("stockInNo", store.getId());
//            List<Map<String, Object>> stockDetailList = new ArrayList<>();
//            Map<String, Object> stockDetail = new HashMap<>();
//            stockDetail.put("stockDetailId", "IN2023102201");
//            stockDetail.put("skuId", ids[i]);
//            stockDetail.put("skuCode", "9874521");
//            stockDetail.put("skuName", "螺丝");
//            stockDetail.put("expectedCount", overfill1[i]);
//            // 将stockDetail添加到stockDetailList中
//            stockDetailList.add(stockDetail);
//            // 将stockDetailList添加到外层Map中
//            outerMap.put("stockDetailList", stockDetailList);
//            HttpUtil.postFile("/api/business/stockIn/create",outerMap);

            int y=dao.insertStoreDetails1(ids[i],overfill1[i],store.getId(),container[i].split(","));

        }
        return null;
    }

    @Override
    public JSONObject getStoreDetailsList(String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<storeDetails> list=dao.getStoreDetailsList(id);
        for(storeDetails c:list){
            js.put("id",c.getId());
            js.put("productCode",c.getProductCode());
            js.put("productName",c.getProductName());
            js.put("expectedCount",c.getExpectedCount());
            js.put("actualCount",c.getActualCount());
            arr.add(js);
        }
        int x= dao.getStoreDetailsListCount(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject activation(String id) {
        JSONObject js=new JSONObject();
//        Map<String, String> map = new HashMap<>();
//        map.put("stockInNo",id);
//        String xy=HttpUtil.post("/api/business/stockIn/activate",map);
        int x=dao.activation(id);
        if (x>0){
            js.put("code","suss");
        }else {
            js.put("code","1066");
        }

        return js;
    }

    @Override
    public JSONObject DelStore(String id) {
        JSONObject js=new JSONObject();
        int x=dao.DelStore(id);
            js.put("code","suss");

        return js;
    }

    @Override
    public JSONObject getOutboundList(int page, int limit) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<outBound> list=dao.getOutboundList((page-1)*limit,limit);
        for(outBound c:list){
            js.put("id",c.getId());
            js.put("outBoundId",c.getOutBoundId());
            js.put("device",c.getDevice());
            js.put("dept",c.getDept());
            js.put("workbenches",c.getWorkbenches());
            js.put("status",c.getStatus());
            js.put("username",c.getUsername());
            js.put("updateAtStr",c.getUpdateAtStr());
            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getOutboundListCount();
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getOutboundDetailsList(String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<outBoundDetails> list=dao.getOutboundDetailsList(id);
        for(outBoundDetails c:list){
            js.put("id",c.getId());
            js.put("productCode",c.getProductCode());
            js.put("productName",c.getProductName());
            js.put("expectedCount",c.getExpectedCount());
            js.put("actualCount",c.getActualCount());
            arr.add(js);
        }
        int x= dao.getOutboundDetailsListCount(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getContainerList2(int page, int limit, String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<container> list=dao.getContainerList2(id);
        for(container c:list){
            js.put("id",c.getId());
            js.put("ContainerCode",c.getContainerCode());
            js.put("device",c.getDevice());
            js.put("actualCount",c.getActualCount());
//            js.put("arrange",c.getArrange());
//            js.put("row",c.getRow());
//            js.put("status",c.getStatus());
            js.put("usageRate",c.getUsageRate());
//            js.put("updateAtStr",c.getUpdateAtStr());
//            js.put("createdAtStr",c.getCreatedAtStr());
            arr.add(js);
        }
        int x= dao.getContainerListCount2(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getCheckDetailsList(String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<checkDetails> list=dao.getCheckDetailsList(id);
        for(checkDetails c:list){
            js.put("id",c.getId());
            js.put("productCode",c.getProductCode());
            js.put("productName",c.getProductName());
            js.put("inventoryNum",c.getInventoryNum());
            arr.add(js);
        }
        int x= dao.getCheckDetailsListCount(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getContainerList3(int page, int limit, String id) {
        JSONObject js=new JSONObject();
        JSONArray arr=new JSONArray();
        List<container> list=dao.getContainerList3(id);
        for(container c:list){
            js.put("id",c.getId());
            js.put("ContainerCode",c.getContainerCode());
            js.put("device",c.getDevice());
            js.put("actualCount",c.getActualCount());
            js.put("usageRate",c.getUsageRate());
            arr.add(js);
        }
        int x= dao.getContainerListCount3(id);
        js.put("count", x);
        js.put("code",0);
        js.put("data",arr);
        return js;
    }

    @Override
    public JSONObject getoutboundRecordCount(int pageNum, int pageSize) {
        JSONObject allJs = new JSONObject(); // 最外层的JSON对象
        JSONObject dataJs = new JSONObject(); // "data"字段对应的JSON对象
        JSONObject resultJs = new JSONObject(); // "result"字段对应的JSON对象
        JSONArray recordsArr = new JSONArray(); // "records"字段对应的JSON数组
        List<outboundRecord> list = dao.getoutboundRecord((pageNum - 1) * pageSize, pageSize);
        for (outboundRecord c : list) {
            JSONObject recordJs = new JSONObject(); // 每个出库记录对应的JSON对象
            recordJs.put("stockOutNo", c.getStockOutNo());
            recordJs.put("finishDate", c.getFinishDate()); // 假设getFinishDate()返回的是Date类型，需要转换为时间戳
            List<stockDetail> suppliesList = dao.getSuppliesList(c.getId());
            JSONArray detailArr = new JSONArray(); // 每个出库记录的详情列表对应的JSON数组

            for (stockDetail s : suppliesList) {
                JSONObject detailJs = new JSONObject(); // 每个详情对应的JSON对象
                detailJs.put("stockDetailId", s.getStockDetailId());
                detailJs.put("skuId", s.getSkuId());
                detailJs.put("skuCode", "");
                detailJs.put("skuName", s.getSkuName());
                detailJs.put("expectedCount", s.getExpectedCount());
                detailJs.put("actualCount", s.getActualCount());
                detailArr.add(detailJs);
            }

            recordJs.put("stockDetailList", detailArr);
            recordsArr.add(recordJs);
        }

        int totalCount = dao.getoutboundRecordCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize); // 计算总页数，使用Math.ceil确保有余数时页数加一
        resultJs.put("records", recordsArr);
        resultJs.put("total", totalCount);
        resultJs.put("current", pageNum);
        resultJs.put("pages", totalPages);
        resultJs.put("size", pageSize);
        dataJs.put("errcode", "0");
        dataJs.put("errmsg", "请求成功");
        dataJs.put("result", resultJs);
        allJs.put("code", 200);
        allJs.put("success", true);
        allJs.put("data", dataJs);

        return allJs;
    }

    @Override
    public JSONObject getStockInCount(int pageNum, int pageSize) {
        JSONObject allJs = new JSONObject(); // 最外层的JSON对象
        JSONObject dataJs = new JSONObject(); // "data"字段对应的JSON对象
        JSONObject resultJs = new JSONObject(); // "result"字段对应的JSON对象
        JSONArray recordsArr = new JSONArray(); // "records"字段对应的JSON数组
        List<outboundRecord> list = dao.getStockIn((pageNum - 1) * pageSize, pageSize);
        for (outboundRecord c : list) {
            JSONObject recordJs = new JSONObject(); // 每个出库记录对应的JSON对象
            recordJs.put("stockOutNo", c.getStockOutNo());
            recordJs.put("finishDate", c.getFinishDate()); // 假设getFinishDate()返回的是Date类型，需要转换为时间戳
            List<stockDetail> suppliesList = dao.getStockInSuppliesList(c.getId());
            JSONArray detailArr = new JSONArray(); // 每个出库记录的详情列表对应的JSON数组

            for (stockDetail s : suppliesList) {
                JSONObject detailJs = new JSONObject(); // 每个详情对应的JSON对象
                detailJs.put("stockDetailId", s.getStockDetailId());
                detailJs.put("skuId", s.getSkuId());
                detailJs.put("skuCode", "");
                detailJs.put("skuName", s.getSkuName());
                detailJs.put("expectedCount", s.getExpectedCount());
                detailJs.put("actualCount", s.getActualCount());
                detailArr.add(detailJs);
            }
            recordJs.put("stockDetailList", detailArr);
            recordsArr.add(recordJs);
        }
        int totalCount = dao.getStockInCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize); // 计算总页数，使用Math.ceil确保有余数时页数加一

        resultJs.put("records", recordsArr);
        resultJs.put("total", totalCount);
        resultJs.put("current", pageNum);
        resultJs.put("pages", totalPages);
        resultJs.put("size", pageSize);

        dataJs.put("errcode", "0");
        dataJs.put("errmsg", "请求成功");
        dataJs.put("result", resultJs);

        allJs.put("code", 200);
        allJs.put("success", true);
        allJs.put("data", dataJs);

        return allJs;

    }
}
